package com.wangzh.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangzh.controller.util.R;
import com.wangzh.entity.Book;
import com.wangzh.service.IBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping(value = "books")
public class BookController {

    @Autowired
    private IBookService bookService;

//    @Autowired
//    private IpCountService ipCountService;

    @GetMapping
    public String getList() {
        System.out.println("qqq");
        log.debug("debuf");
        log.warn("warn");
        log.info("info");
        log.error("error");
        return "";
    }

    @PostMapping(value = "/page/{size}/{current}")
    public R page(@RequestBody Book book,
                  @PathVariable(value = "size") Integer size,
                  @PathVariable(value = "current") Integer current) {
//        ipCountService.count();
        IPage<Book> page = new Page(current, size);

        IPage<Book> page1 = bookService.getPage(page, book);
        if (current > page1.getPages()) {
            page = new Page(page.getPages(), size);
            page1 = bookService.getPage(page, book);
        }
        return new R(true, page1);
    }

    @GetMapping(value = "{id}")
    public R getById(@PathVariable Integer id) {
        log.debug("debug----");
        log.info("info----");
        log.warn("warn------");
        log.error("error------");
        return new R(true, bookService.getById(id));
    }

    @DeleteMapping(value = "{id}")
    public R deleteById(@PathVariable Integer id) {
        return new R(bookService.delete(id));

    }

    @PostMapping
    public R save(@RequestBody Book book) throws IOException {

        return new R(bookService.save(book));
    }

    @PutMapping
    public R update(@RequestBody Book book) {
        return new R(bookService.updateById(book));
    }


}
